/**
 * Author: Brent Myers
 * Time-stamp: <2008-07-23 12:40:03 bam>
 * 
 * Linear Probing Hashtable Tests
 * 
 */

#include <boost/test/unit_test.hpp>

#include "src/LinearProbe.h"
#include "src/SimpleHash.h"

#include <utility>

using std::make_pair;

using namespace genhash;

struct LPTestConfig1 
{
	enum { max_size = 10 };
	typedef int KeyType;
	typedef int DataType;
	typedef SimpleHash<LPTestConfig1> HashFn;
	typedef LinearProbe<LPTestConfig1> CollisionStrategy;
};
typedef LPTestConfig1::CollisionStrategy Strat1;

struct LPTestConfig2
{
	enum { max_size = 2 };
	typedef int KeyType;
	typedef int DataType;
	typedef SimpleHash<LPTestConfig2> HashFn;
	typedef LinearProbe<LPTestConfig2> CollisionStrategy;
};
typedef LPTestConfig2::CollisionStrategy Strat2;

BOOST_AUTO_TEST_CASE( lp1 )
{
	Strat1 ht;
	ht.insert(make_pair(1, 2));
	Strat1::iterator it(ht.find(1));
	BOOST_CHECK_EQUAL(it->first, 1);
	BOOST_CHECK_EQUAL(it->second, 2);
}


BOOST_AUTO_TEST_CASE( lp2 )
{
	Strat1 ht;
	ht.insert(make_pair(1, 2));
	ht.insert(make_pair(3, 4));

	Strat1::iterator it1(ht.find(1));
	BOOST_CHECK_EQUAL(it1->first, 1);
	BOOST_CHECK_EQUAL(it1->second, 2);

	Strat1::iterator it2(ht.find(3));
	BOOST_CHECK_EQUAL(it2->first, 3);
	BOOST_CHECK_EQUAL(it2->second, 4);

}

BOOST_AUTO_TEST_CASE( lp3 )
{
	Strat1 ht;
	ht.insert(make_pair(1, 2));
	ht.insert(make_pair(11, 12));

	Strat1::iterator it1(ht.find(1));
	BOOST_CHECK_EQUAL(it1->first, 1);
	BOOST_CHECK_EQUAL(it1->second, 2);

	Strat1::iterator it2(ht.find(11));
	BOOST_CHECK_EQUAL(it2->first, 11);
	BOOST_CHECK_EQUAL(it2->second, 12);

}

BOOST_AUTO_TEST_CASE( lp4 )
{
	Strat2 ht;
	ht.insert(make_pair(1, 2));
	ht.insert(make_pair(11, 12));

	Strat1::iterator it1(ht.find(1));
	BOOST_CHECK_EQUAL(it1->first, 1);
	BOOST_CHECK_EQUAL(it1->second, 2);

	Strat1::iterator it2(ht.find(11));
	BOOST_CHECK_EQUAL(it2->first, 11);
	BOOST_CHECK_EQUAL(it2->second, 12);

}

BOOST_AUTO_TEST_CASE( lp5 )
{
	Strat2 ht;
	BOOST_CHECK_NO_THROW(ht.insert(make_pair(1, 2)));
	BOOST_CHECK_NO_THROW(ht.insert(make_pair(11, 12)));
	BOOST_CHECK_THROW(ht.insert(make_pair(4, 5)), HashtableFullException);
}

BOOST_AUTO_TEST_CASE( lp6 )
{
	Strat1 ht;
	ht.insert(make_pair(1, 2));

	Strat1::iterator it1(ht.find(1));
	BOOST_CHECK_EQUAL(it1->first, 1);
	BOOST_CHECK_EQUAL(it1->second, 2);

	ht.insert(make_pair(1, 12));

	Strat1::iterator it2(ht.find(1));
	BOOST_CHECK_EQUAL(it2->first, 1);
	BOOST_CHECK_EQUAL(it2->second, 2);

}

BOOST_AUTO_TEST_CASE( lp7 )
{
	Strat1 ht;
	ht.insert(make_pair(1, 2));

	Strat1::iterator it1(ht.find(2));
	BOOST_CHECK(it1 == ht.end());
}


